package cz.muni.fi.pa165.hauntedhouses.web;

import cz.muni.fi.pa165.hauntedhouses.dto.AbilityDTO;
import cz.muni.fi.pa165.hauntedhouses.dto.GhostDTO;
import cz.muni.fi.pa165.hauntedhouses.dto.HouseDTO;
import cz.muni.fi.pa165.hauntedhouses.dto.HumanDTO;
import cz.muni.fi.pa165.hauntedhouses.facade.ServiceFacade;
import cz.muni.fi.pa165.hauntedhouses.facade.ServiceFacadeImpl;
import java.util.List;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Q.90
 */
@UrlBinding("/jsp/play")
public class PlayActionBean implements ActionBean {

    final static Logger log = LoggerFactory.getLogger(GhostsActionBean.class);
    private ActionBeanContext context;
    
    @SpringBean
    protected ServiceFacade sf;


    public List<GhostDTO> getGhosts() {
        return sf.findAllGhosts();
    }

    public List<AbilityDTO> getAbilities() {
        return ghost.getAbilities();
    }

    public List<HumanDTO> getHumans() {
        return sf.findHumansByHouse(house);
    }

    public List<HouseDTO> getHouses() {
        return sf.findAllHouses();
    }
    
    @ValidateNestedProperties(value = {
            @Validate(on = {"play2"}, field = "id", required = true),
    })
    private HouseDTO house;
    
    @ValidateNestedProperties(value = {
            @Validate(on = {"play3"}, field = "id", required = true),
    })
    private HumanDTO human;
    
    @ValidateNestedProperties(value = {
            @Validate(on = {"play2","play3"}, field = "id", required = true),
    })
    private GhostDTO ghost;
    
    @ValidateNestedProperties(value = {
            @Validate(on = {"play3"}, field = "id", required = true),
    })
    private AbilityDTO ability;
    
    private int diffExp;
    private int diffPsyche;
    private int diffLvl;

    public ServiceFacade getSf() {
        return sf;
    }

    public void setSf(ServiceFacadeImpl sf) {
        this.sf = sf;
    }

    public int getDiffLvl() {
        return diffLvl;
    }

    public void setDiffLvl(int diffLvl) {
        this.diffLvl = diffLvl;
    }

    public int getDiffExp() {
        return diffExp;
    }

    public void setDiffExp(int diffExp) {
        this.diffExp = diffExp;
    }

    public int getDiffPsyche() {
        return diffPsyche;
    }

    public void setDiffPsyche(int diffPsyche) {
        this.diffPsyche = diffPsyche;
    }

    public HouseDTO getHouse() {
        return house;
    }

    public void setHouse(HouseDTO house) {
        this.house = house;
    }

    public HumanDTO getHuman() {
        return human;
    }

    public void setHuman(HumanDTO human) {
        this.human = human;
    }

    public GhostDTO getGhost() {
        return ghost;
    }

    public void setGhost(GhostDTO ghost) {
        this.ghost = ghost;
    }

    public AbilityDTO getAbility() {
        return ability;
    }

    public void setAbility(AbilityDTO ability) {
        this.ability = ability;
    }
    
    @DefaultHandler
    public Resolution play() {
        return new ForwardResolution("/WEB-INF/jsp/play.jsp");
    }
    
    public Resolution home() {
        return new ForwardResolution("/WEB-INF/jsp/home.jsp");
    }

    public Resolution play2() {
        return new ForwardResolution("/WEB-INF/jsp/play2.jsp");
    }

    public Resolution play3() {
        int exp = ghost.getExperience();
        int psych = human.getPsyche();
        int lvl = ghost.getLvl();
        sf.useAbility(ability, human, ghost);
        diffExp = ghost.getExperience() - exp;
        diffPsyche = human.getPsyche() - psych;
        diffLvl = ghost.getLvl() - lvl;
        return new ForwardResolution("/WEB-INF/jsp/play3.jsp");
    }

    public Resolution chooseAbility() {
        return new ForwardResolution("/WEB-INF/jsp/ghostedit.jsp");
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"play2","play3"})
    public void loadGhostFromDatabase() {
        String ids = context.getRequest().getParameter("ghost.id");
        if (ids == null) {
            return;
        }
        ghost = sf.getGhost(Long.parseLong(ids));
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"play2","play3"})
    public void loadHouseFromDatabase() {
        String ids = context.getRequest().getParameter("house.id");
        if (ids == null) {
            return;
        }
        house = sf.getHouse(Long.parseLong(ids));
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"play3"})
    public void loadAbilityFromDatabase() {
        String ids = context.getRequest().getParameter("ability.id");
        if (ids == null) {
            return;
        }
        ability = sf.getAbility(Long.parseLong(ids));
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"play3"})
    public void loadHumanFromDatabase() {
        String ids = context.getRequest().getParameter("human.id");
        if (ids == null) {
            return;
        }
        human = sf.getHuman(Long.parseLong(ids));
    }
}
